﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;


namespace CCPSIP.User
{
    /// <summary>CCPSIP_038：User 移除個人圖片(大頭照) API</summary>
    /// idHeader = 裝置 UUID 前八碼。
    /// checkcode = 裝置限時驗證碼。

    public class icon_remove : IHttpHandler
    {
        private string kIconDirLatest = ChrisKit.RootDir + "CCPSIP\\UserIcon\\Latest"; //ios.ccp
        private string kIconPathDefaultUser = ChrisKit.RootDir + "CCPSIP\\UserIcon\\Default\\User.jpg";  //ios.ccp
        private string kIconPathDefaultRobot = ChrisKit.RootDir + "CCPSIP\\UserIcon\\Default\\Robot.jpg";//ios.ccp


        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            resp.ContentType = "text/plain";
            resp.ContentEncoding = System.Text.Encoding.UTF8;
            DateTime reqTime = DateTime.Now;
            string resultStr = "";


            try
            {
                //【01】取得POST參數，驗證身分
                //
                string idHeader = req.Form["idHeader"];
                string checkcode = req.Form["checkcode"];

                SipUserInfo userInfo = SipKit.GetUserInfo(idHeader, checkcode, req);
                if (userInfo == null)
                {
                    throw new Exception("Error_01: Cannot identify this device");//無法識別此裝置
                }


                //【02】移除個人圖片檔案
                //
                string latestIconPath = string.Format(@"{0}\{1}.jpg", kIconDirLatest, userInfo.SIP_Num);

                for (int i = 0; i < 5; i++)
                {
                    try
                    {
                        if (File.Exists(latestIconPath))
                        {
                            File.Delete(latestIconPath);
                        }
                        break;
                    }
                    catch
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                }

                if (File.Exists(latestIconPath))
                {
                    throw new Exception("Error_02: The icon file cannot be removed");//檔案鎖住了，無法移除
                }


                //【03】記錄個人圖片更新時間
                //
                if (RobotKit.isRobot(userInfo.SIP_Num))
                {
                    File.Copy(kIconPathDefaultRobot, latestIconPath, true);
                }
                else
                {
                    File.Copy(kIconPathDefaultUser, latestIconPath, true);
                }

                long iconSize = new FileInfo(latestIconPath).Length;
                OracleKitUser.UpdateIconTime(userInfo.SIP_Num, DateTime.Now, Convert.ToInt32(iconSize));


                //【99】回傳結果
                //
                resultStr = "OK";
            }
            catch (Exception ex)
            {
                resp.ContentType = "application/xml";
                resultStr = ChrisKit.Ex2Xml(ex, false);
            }
            finally
            {
                int contentLength = resp.ContentEncoding.GetByteCount(resultStr);
                resp.AppendHeader("Content-Length", contentLength.ToString());
                resp.Write(resultStr);
                resp.Flush();
                resp.Close();
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}
